/**
 * 存放系统级别的store
 */
import { ThemeApi } from '@/api/ThemeApi';
import { LoginApi } from '@/api/LoginApi';

export default {
  namespaced: true,
  state: {
    // 当前系统是否已经初始化过配置
    alreadyInitConfig: null,
    // 后台管理系统左上角Logo
    gunsMgrLogo: null,
    // 后台管理系统名称
    gunsMgrName: 'Guns Tech.',
    // 备案编号
    gunsMgrBeiNo: null,
    // 备案跳转url
    gunsMgrBeiUrl: 'https://beian.miit.gov.cn/',
    // favicon图标地址
    gunsMgrFavicon: null,
    // 页脚文字
    gunsMgrFooterText: null,
    // 登录页面背景图片
    gunsMgrLoginBackgroundImg: null,
    //api基础路径
    baseUrl: ''
  },
  mutations: {
    /**
     * 更新初始化状态
     *
     * @param state vuex状态
     * @param {Boolean} initFlag 是否初始化了系统
     * @author fengshuonan
     * @date 2021/6/12 19:20
     */
    updateInitFlag(state, initFlag) {
      state.alreadyInitConfig = initFlag;
    },

    /**
     * 更新初始化状态
     *
     * @param state vuex状态
     * @param {object} systemInfo 系统的主题详情
     * @author fengshuonan
     * @date 2021/6/12 19:20
     */
    updateSystemInfo(state, systemInfo) {
      state.gunsMgrLogo = systemInfo.gunsMgrLogo;
      state.gunsMgrName = systemInfo.gunsMgrName;
      state.gunsMgrBeiNo = systemInfo.gunsMgrBeiNo;
      state.gunsMgrBeiUrl = systemInfo.gunsMgrBeiUrl;
      state.gunsMgrFavicon = systemInfo.gunsMgrFavicon;
      state.gunsMgrFooterText = systemInfo.gunsMgrFooterText;
      state.gunsMgrLoginBackgroundImg = systemInfo.gunsMgrLoginBackgroundImg;
    },

    /**
     * 设置基础url
     *
     * @author fengshuonan
     * @date 2022/3/4 10:37
     */
    setBaseUrl(state, data) {
      state.baseUrl = data;
    }
  },
  actions: {
    /**
     * 更新初始化状态
     *
     * @param state vuex状态
     * @param {Boolean} initFlag 是否初始化了系统
     * @author fengshuonan
     * @date 2021/6/12 19:20
     */
    updateInitFlag({ commit }, initFlag) {
      commit('updateInitFlag', initFlag);
    },

    /**
     * 加载后台主题配置
     *
     * @author fengshuonan
     * @date 2022/1/12 14:48
     */
    loadThemeInfo({ commit, state }) {
      return new Promise(resolve => {
        if (state.gunsMgrLogo != null) {
          resolve(Object.assign({}, state));
        } else {
          ThemeApi.getCurrentThemeInfo().then(result => {
            commit('updateSystemInfo', result);
            resolve(result);
          });
        }
      });
    },

    /**
     * 获取后端的基础url
     * @param commit
     * @param baseUrl {Array<String>} 路径
     */
    getBaseUrl({ commit }) {
      return new Promise(resolve => {
        LoginApi.getBackendDeployUrl().then(result => {
          commit('setBaseUrl', result);
          resolve(result);
        });
      });
    }
  },
  getters: {
    /**
     * 获取是否初始化了配置
     *
     * @author fengshuonan
     * @date 2021/7/9 17:06
     */
    alreadyInitConfig: state => {
      return state.alreadyInitConfig;
    }
  }
};
